package com.xz.stu.server.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.stu.server.entity.Holiday;
import com.xz.stu.server.entity.Term;
import com.xz.stu.server.mapper.HolidayMapper;
import com.xz.stu.server.service.HolidayService;
import com.xz.stu.server.service.TermService;
import com.xz.stu.server.vo.holiday.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 熊磊
* @description 针对表【school_holiday(节假日表)】的数据库操作Service实现
* @createDate 2025-02-26 15:34:32
*/
@Service
public class HolidayServiceImpl extends ServiceImpl<HolidayMapper, Holiday>
    implements HolidayService {
    @Resource
    private TermService termService;

    @Override
    public Long createHoliday(HolidayCreateReqVO createReqVO) {
        Holiday holiday = BeanUtils.toBean(createReqVO, Holiday.class);
        if (Objects.isNull(holiday.getTotalDays()) || holiday.getTotalDays() == 0){
            holiday.setTotalDays(1);
        }
        //根据假期日期查询学期,大于学期开始时间，小于学期结束时间
        Term one = termService.lambdaQuery()
                .ge(Term::getStartDate, holiday.getHolidayDate())
                .le(Term::getEndDate, holiday.getHolidayDate())
                .one();
        if (one == null){
            //获去一个最近的时间
            one = termService.lambdaQuery()
                    .ge(Term::getStartDate, holiday.getHolidayDate())
                    .orderByAsc(Term::getStartDate)
                    .last("limit 1")
                    .one();
        }
        holiday.setTermId(one.getId());
        this.save(holiday);
        return holiday.getId();
    }

    @Override
    public void updateHoliday(HolidayUpdateReqVO updateReqVO) {
        Holiday holiday = BeanUtils.toBean(updateReqVO, Holiday.class);
        this.updateById(holiday);
    }

    @Override
    public void deleteHoliday(List<Long> id) {
        this.removeByIds(id);
    }

    @Override
    public HolidayRespVO getHoliday(Long id) {
        Holiday holiday = this.getById(id);
        if (holiday != null) {
            return BeanUtils.toBean(holiday, HolidayRespVO.class);
        }
        throw new RuntimeException("节假日不存在");
    }

    @Override
    public List<HolidayRespVO> getHolidayList(HolidayListReqVO listReqVO) {
        List<HolidayRespVO> holidayRespVOList = this.lambdaQuery()
                .between(Holiday::getHolidayDate, listReqVO.getStartDate(), listReqVO.getEndDate())
                .list()
                .stream()
                .map(holiday -> BeanUtils.toBean(holiday, HolidayRespVO.class))
                .collect(Collectors.toList());
        Term one = termService.lambdaQuery()
                .eq(Term::getTermCode, listReqVO.getTermCode())
                .one();
        holidayRespVOList.forEach(calendarEventRespVO -> {
            calendarEventRespVO.setTermId(one.getId());
        });
        return holidayRespVOList;
    }

    @Override
    public PageResult<HolidayRespVO> getHolidayListPage(HolidayListPageReqVO listReqVO) {
        LambdaQueryWrapper<Holiday> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(listReqVO.getName()), Holiday::getName, listReqVO.getName())
                .eq(Objects.nonNull(listReqVO.getType()), Holiday::getType, listReqVO.getType());
        Page<Holiday> page = this.page(new Page<>(listReqVO.getPageNo(), listReqVO.getPageSize()), queryWrapper);
        if (page != null) {
            PageResult<HolidayRespVO> pageResult = new PageResult<>(BeanUtils.toBean(page.getRecords(), HolidayRespVO.class), page.getTotal());
            return pageResult;
        }
        return null;
    }
}




